-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add SiteURI class #7252
feat: add SiteURI class #7252
Conversation
2d602ea
to
87c90ca
Compare
Maybe this is a silly question, but... as I understand, this new class will bring full PSR-7 compatibility, right? I don't see any related changes in the framework so we won't rely on it. The question is, who needs this class and for what? Like... I saw your description:
But I'm afraid I don't fully understand the use cases. |
This PR only adds a new SiteURI class, and does not change the current framework behavior yet. In the next PR(s), I will do:
About compatibility with PSR-7, the URI class will be cleaner because site-related functions like baseURL, will be separated into the SiteURI class. And the URI class will be easier to change. Finally, I think |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am obviously in favor of this but I want to be sure we get reviews from @lonnieezell and @iRedds and anyone else who was opposed to the idea originally.
I was purposefully not commenting since my position was already known and it seemed I was overruled :) I'm confused why we need a second class that does 99% of the same things as the URI class. The URI class is just for working with URIs in general, crafted to meet the RFC as best we could at the time. It seems like all other issues should be fixed where it's used, or the base class should be changed to support our needs. The implementation itself is solid enough. Though if we're going to include it, we need docs and guidance on when to use which one. It seems strange to me to make this new class and we're not using it ourselves in |
I agree with @lonnieezell. I don't understand why we need a second class to handle URLs. Can somebody give me a clear example of where it will be useful? AFAIK the only issue that we have now with the URI class is that it does not follow PSR-7 (it's something path related). Also, if the goal for both classes is to follow the PSR-7, what will be the difference? Wouldn't it be better to see some additional methods added to the current class instead? |
Because we have already two kinds of concept URI,
SiteURI has its own properties like Now we are already confusing 1. and 2. So we have two kinds of URI segments for one SiteURL. |
This fix (and refactoring) will be long story. This PR is just the first one. |
I won't repeat my arguments, the original PR is here with discussion: #4647 I would like to point out that after doing a deep dive on URI handling and bug fixes both @kenjis and I came to the same conclusion, independently. I think the problem is larger than credited and may indeed merit the potential confusion/clutter of an additional class. |
After giving this a bit more thought, I think I'm ok with this on 2 conditions:
|
f65a55c
to
72f76a6
Compare
$uri = $this->request->getUri();
echo (string) $uri; // "http://localhost:8888/ci431/public/test?a=b" → Okay
echo $uri->getPath(); // "test" → NG. It should be "/ci431/public/test" when following PSR-7 And I think that such behavior corresponds to PSR. // for http://localhost:8888/ci431/public/test?a=b
URI::getPath(); // => /ci431/public/test
// for http://localhost:8888/{base_path}/test?a=b
URI::getPath(); // => test (without leading slash, as an indicator that the base path is being used.)
// Accordingly, the leading slash will determine whether the path is absolute or rootless
// for http://localhost:8888/{base_path}/test?a=b
URI::withPath('/xxx'); // http://localhost:8888/xxx?a=b
// for http://localhost:8888/{base_path}/test?a=b
URI::withPath('xxx'); // http://localhost:8888/{base_path}/xxx?a=b |
What is // for http://localhost:8888/{base_path}/test?a=b
URI::getPath(); // => test (without leading slash, as an indicator that the base path is being used.) |
@kenjis app.baseURL = 'http://localhost:8888{/ci431/public/}' <- base path |
I don't get it. |
There are no objects in PSR-7. Interfaces only. That is, mandatory public methods for PSR compliance. |
https://www.php-fig.org/psr/psr-7/ This means like this?
|
<?php
require __DIR__ . '/vendor/autoload.php';
use League\Uri\Http;
$uri = Http::createFromBaseUri('foo/bar', 'http://localhost/ci4/');
echo $uri . PHP_EOL; // http://localhost/ci4/foo/bar
echo $uri->getPath() . PHP_EOL; // /ci4/foo/bar |
use League\Uri\Http;
$uri = Http::createFromBaseUri('foo/bar', 'http://localhost/ci4/');
$uri2 = $uri->withPath('controller/method');
|
use Slim\Psr7\Factory\UriFactory;
$factory = new UriFactory();
$uri = $factory->createUri('http://localhost/ci4/');
$uri2 = $uri->withPath('controller/method');
echo $uri2 . PHP_EOL; // http://localhost/controller/method
echo $uri2->getPath() . PHP_EOL; // controller/method
$uri2 = $uri->withPath('/controller/method');
echo $uri2 . PHP_EOL; // http://localhost/controller/method
echo $uri2->getPath() . PHP_EOL; // /controller/method |
use Nyholm\Psr7\Uri;
$uri = new Uri('http://localhost/ci4/');
$uri2 = $uri->withPath('controller/method');
echo $uri2 . PHP_EOL; // http://localhost/controller/method
echo $uri2->getPath() . PHP_EOL; // controller/method
$uri2 = $uri->withPath('/controller/method');
echo $uri2 . PHP_EOL; // http://localhost/controller/method
echo $uri2->getPath() . PHP_EOL; // /controller/method |
empty string |
Change the condition to add / after index.php.
getSegment() accepts n+1.
For consistency with previous implementations.
The $routePath never starts with `/`. If the path is `/`, the URI retain the trailing `/`.
If the path ends with `/`, the URI retain the trailing `/`.
SiteURI will check.
The parent method is fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! I feel validated in the need for this given how much code is in SiteURI on top of URI. I appreciate your verbosity in naming variables and methods - it is easy to overlook pieces which has led to many bugs in the past. I think this will be a good step forward.
I assume the URL Helper methods get changed in the next PR? I haven't looked at that one yet...
@TimexPeachtree @MGatner Thank you for approvals. |
Description
This PR is needed for #7123
We need two URI classes. One for general purpose URI class, and the other is for URI of the app site.
$routePath
never starts with/
./
, the URI retains the trailing/
.getPath()
returns the full URI path.getRoutePath()
returns the path relative to baseURL.Related #5930, #7239, #7249, #7251
Checklist: